Ext.ns("org.cancerregulome.molotov.js");

org.cancerregulome.molotov.js.History = Ext.extend(Object, {
    tableId: null,
    queryUri: null,

    constructor: function(config) {
        Ext.apply(this, config);

        org.cancerregulome.molotov.js.History.superclass.constructor.call(this);

        this.read();
    },

    setTableId: function(tId) {
        if (tId) {
            this.tableId = tId;
            this.make();
        }
    },

    setQueryUri: function(qUri) {
        if (qUri) {
            this.queryUri = qUri;
            this.make();
        }
    },

    make: function() {
        var token = "";
        if (this.tableId) {
            token += "t_" + this.tableId + ":";
        }
        if (this.queryUri) {
            token += "q_" + this.queryUri + ":";
        }

        if (token.length > 0) {
            Ext.History.add(token, true);
        }
    },

    read: function() {
        var token = Ext.History.getToken();
        if (token) {
            var splits = token.split(":");
            if (splits) {
                Ext.each(splits, function(splt) {
                    if (splt) {
                        if (splt.indexOf("t_") >= 0) {
                            this.tableId = splt.replace("t_", "");
                        } else if (splt.indexOf("q_") >= 0) {
                            this.queryUri = splt.replace("q_", "");
                        }
                    }
                }, this);
            }
        }
    }
});
